package org.theonept.travian.model;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.theonept.travian.util.HttpUtil;

public class Ressource extends AbstractBatiment {

	private Integer production;

	Production productionEntity;

	/**
	 * @return the production
	 */
	public Integer getProduction() {
		return production;
	}

	/**
	 * @param production
	 *            the production to set
	 */
	public void setProduction(Integer production) {
		this.production = production;
	}

	/**
	 * @return the costCereal
	 */
	public Integer getCostCereal() {
		return costCereal;
	}

	static List<Ressource> getRessources(Production production) {
		List<Ressource> ressources = new ArrayList<Ressource>();

		Document documentRessource = HttpUtil
				.getDocumentFromUrl(production.getVillage().getCompte()
						.getUrlServeur()
						+ "dorf1.php?=newdid="
						+ production.getVillage().getIdVillage());
		List<Element> elementResss = documentRessource.getElementById(
				"village_map").getElementsByAttributeValueStarting("class",
				"level");

		for (int i = 0; i < elementResss.size(); i++) {
			Element elementRess = elementResss.get(i);
			String[] classValue = elementRess.attr("class").split(" ");
			if (classValue[1].equals("gid1")) {
				Bois ressource = new Bois();
				ressource.setVillage(production.getVillage());
				ressource.setProductionEntity(production);
				ressource.setId(i + 1 + "");
				ressource.setLevel(elementRess.text());
				ressources.add(ressource);
				Document documentRessourcePrecise = HttpUtil
						.getDocumentFromUrl(production.getVillage().getCompte()
								.getUrlServeur()
								+ "build.php?id=" + (i + 1));

				Element elementProduction = documentRessourcePrecise
						.getElementById("build_value")
						.getElementsByAttributeValue("class", "currentLevel")
						.first().getElementsByAttributeValue("class", "number")
						.first();
				ressource.setProduction(Production.getInt(elementProduction
						.text()));
				Elements cost = documentRessourcePrecise.getElementById(
						"contract").getElementsByAttributeValue("class",
						"showCosts");
				if (!cost.isEmpty()) {
					List<Element> elementUpProduction = cost.first().children();
					for (Element element : elementUpProduction) {
						if (element.attr("class").contains("r1")) {
							ressource.setCostBois(Production.getInt(element
									.text()));
						} else if (element.attr("class").contains("r2")) {
							ressource.setCostArgile(Production.getInt(element
									.text()));
						} else if (element.attr("class").contains("r3")) {
							ressource.setCostFer(Production.getInt(element
									.text()));
						} else if (element.attr("class").contains("r4")) {
							ressource.setCostCereal(Production.getInt(element
									.text()));
						} else if (element.attr("class").contains("r5")) {
							ressource.setConsoCereal(Production.getInt(element
									.text()));
						}
					}
				}

			} else if (classValue[1].equals("gid2")) {
				Argile ressource = new Argile();

				ressource.setVillage(production.getVillage());
				ressource.setProductionEntity(production);
				ressource.setId(i + 1 + "");
				ressource.setLevel(elementRess.text());
				ressources.add(ressource);
				Document documentRessourcePrecise = HttpUtil
						.getDocumentFromUrl(production.getVillage().getCompte()
								.getUrlServeur()
								+ "build.php?id=" + (i + 1));
				Element elementProduction = documentRessourcePrecise
						.getElementById("build_value")
						.getElementsByAttributeValue("class", "currentLevel")
						.first().getElementsByAttributeValue("class", "number")
						.first();
				ressource.setProduction(Production.getInt(elementProduction
						.text()));
				Elements cost = documentRessourcePrecise.getElementById(
						"contract").getElementsByAttributeValue("class",
						"showCosts");
				if (!cost.isEmpty()) {
					List<Element> elementUpProduction = cost.first().children();
					for (Element element : elementUpProduction) {
						if (element.attr("class").contains("r1")) {
							ressource.setCostBois(Production.getInt(element
									.text()));
						} else if (element.attr("class").contains("r2")) {
							ressource.setCostArgile(Production.getInt(element
									.text()));
						} else if (element.attr("class").contains("r3")) {
							ressource.setCostFer(Production.getInt(element
									.text()));
						} else if (element.attr("class").contains("r4")) {
							ressource.setCostCereal(Production.getInt(element
									.text()));
						} else if (element.attr("class").contains("r5")) {
							ressource.setConsoCereal(Production.getInt(element
									.text()));
						}
					}
				}
			} else if (classValue[1].equals("gid3")) {
				Fer ressource = new Fer();

				ressource.setVillage(production.getVillage());
				ressource.setProductionEntity(production);
				ressource.setId(i + 1 + "");
				ressource.setLevel(elementRess.text());
				ressources.add(ressource);
				Document documentRessourcePrecise = HttpUtil
						.getDocumentFromUrl(production.getVillage().getCompte()
								.getUrlServeur()
								+ "build.php?id=" + (i + 1));
				Element elementProduction = documentRessourcePrecise
						.getElementById("build_value")
						.getElementsByAttributeValue("class", "currentLevel")
						.first().getElementsByAttributeValue("class", "number")
						.first();
				ressource.setProduction(Production.getInt(elementProduction
						.text()));
				Elements cost = documentRessourcePrecise.getElementById(
						"contract").getElementsByAttributeValue("class",
						"showCosts");
				if (!cost.isEmpty()) {
					List<Element> elementUpProduction = cost.first().children();
					for (Element element : elementUpProduction) {
						if (element.attr("class").contains("r1")) {
							ressource.setCostBois(Production.getInt(element
									.text()));
						} else if (element.attr("class").contains("r2")) {
							ressource.setCostArgile(Production.getInt(element
									.text()));
						} else if (element.attr("class").contains("r3")) {
							ressource.setCostFer(Production.getInt(element
									.text()));
						} else if (element.attr("class").contains("r4")) {
							ressource.setCostCereal(Production.getInt(element
									.text()));
						} else if (element.attr("class").contains("r5")) {
							ressource.setConsoCereal(Production.getInt(element
									.text()));
						}
					}
				}
			} else if (classValue[1].equals("gid4")) {
				Cereal ressource = new Cereal();

				ressource.setVillage(production.getVillage());
				ressource.setProductionEntity(production);
				ressource.setId(i + 1 + "");
				ressource.setLevel(elementRess.text());
				ressources.add(ressource);
				Document documentRessourcePrecise = HttpUtil
						.getDocumentFromUrl(production.getVillage().getCompte()
								.getUrlServeur()
								+ "build.php?id=" + (i + 1));
				Element elementProduction = documentRessourcePrecise
						.getElementById("build_value")
						.getElementsByAttributeValue("class", "currentLevel")
						.first().getElementsByAttributeValue("class", "number")
						.first();
				ressource.setProduction(Production.getInt(elementProduction
						.text()));
				Elements cost = documentRessourcePrecise.getElementById(
						"contract").getElementsByAttributeValue("class",
						"showCosts");
				if (!cost.isEmpty()) {
					List<Element> elementUpProduction = cost.first().children();
					for (Element element : elementUpProduction) {
						if (element.attr("class").contains("r1")) {
							ressource.setCostBois(Production.getInt(element
									.text()));
						} else if (element.attr("class").contains("r2")) {
							ressource.setCostArgile(Production.getInt(element
									.text()));
						} else if (element.attr("class").contains("r3")) {
							ressource.setCostFer(Production.getInt(element
									.text()));
						} else if (element.attr("class").contains("r4")) {
							ressource.setCostCereal(Production.getInt(element
									.text()));
						} else if (element.attr("class").contains("r5")) {
							ressource.setConsoCereal(Production.getInt(element
									.text()));
						}
					}
				}
			}

		}

		return ressources;
	}

	/**
	 * @return the productionEntity
	 */
	public Production getProductionEntity() {
		return productionEntity;
	}

	/**
	 * @param productionEntity
	 *            the productionEntity to set
	 */
	public void setProductionEntity(Production productionEntity) {
		this.productionEntity = productionEntity;
	}

}
